﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using RearLivestock.Common.Domains.wbx;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.Beginning.Api.Application.Command.PigHistorys
{
    /// <summary>
    /// 猪只历史显示Handler
    /// </summary>
    public class SearchPigHistoryHandler : IRequestHandler<SearchPigHistoryCommand, Result<PageResultDto<PigHistory>>>
    {
        private readonly IMapper mapper;
        /// <summary>
        /// 猪只历史
        /// </summary>
        private readonly IBaseRepository<PigHistory> pigHistoryrepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SearchPigHistoryHandler> _logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="pigHistoryrepository"></param>
        /// <param name="logger"></param>
        public SearchPigHistoryHandler(IMapper mapper, IBaseRepository<PigHistory> pigHistoryrepository, ILogger<SearchPigHistoryHandler> logger)
        {
            this.mapper = mapper;
            this.pigHistoryrepository = pigHistoryrepository;
            _logger = logger;
        }
        /// <summary>
        /// 猪只历史
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<PageResultDto<PigHistory>>> Handle(SearchPigHistoryCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<PageResultDto<PigHistory>>() { Code = (int)CommonErrorCode.Search };
            var page = new PageResultDto<PigHistory>() { TotalCount = 0, PageCount = 0, List = new List<PigHistory>() };
            try
            {
                var pigHistory = await(await pigHistoryrepository.GetAll()).ToListAsync();
                //配种日期开始时间
                if (request.SDate != null)
                {
                    pigHistory = pigHistory.Where(x => x.BreedingDate >= request.SDate).ToList();
                }
                //配种日期结束时间
                if (request.ODate != null)
                {
                    pigHistory = pigHistory.Where(x => x.BreedingDate <= request.ODate).ToList();
                }
                //母亲耳号模糊查
                if (!string.IsNullOrEmpty(request.MotherEarNumber))
                {
                    pigHistory = pigHistory.Where(x => x.MotherEarNumber.Contains(request.MotherEarNumber)).ToList();
                }
                page.TotalCount = pigHistory.Count();
                page.PageCount = (int)Math.Ceiling(page.TotalCount * 1.0 / request.PageSize);
                page.List = pigHistory.OrderBy(x => x.PigHistoryId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Data = page;
                result.Msg = "猪只历史显示成功";
            }
            catch (Exception ex)
            {
                _logger.LogError("猪只历史显示失败" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
